package cn.iocoder.yudao.module.system.service.erp.customerinformation;
import cn.iocoder.yudao.module.system.controller.admin.erp.customerinformation.vo.CustomerInformationPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.erp.customerinformation.vo.CustomerInformationSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.erp.customerinformation.CustomerInformationDO;
import cn.iocoder.yudao.module.system.dal.mysql.erp.customerinformation.CustomerInformationMapper;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.CUSTOMER_INFORMATION_NOT_EXISTS;

/**
 * 客户信息 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class CustomerInformationServiceImpl implements CustomerInformationService {

    @Resource
    private CustomerInformationMapper customerInformationMapper;

    @Override
    public Long createCustomerInformation(CustomerInformationSaveReqVO createReqVO) {
        // 插入
        CustomerInformationDO customerInformation = BeanUtils.toBean(createReqVO, CustomerInformationDO.class);
        customerInformationMapper.insert(customerInformation);
        // 返回
        return customerInformation.getId();
    }

    @Override
    public void updateCustomerInformation(CustomerInformationSaveReqVO updateReqVO) {
        // 校验存在
        validateCustomerInformationExists(updateReqVO.getId());
        // 更新
        CustomerInformationDO updateObj = BeanUtils.toBean(updateReqVO, CustomerInformationDO.class);
        customerInformationMapper.updateById(updateObj);
    }

    @Override
    public void deleteCustomerInformation(Long id) {
        // 校验存在
        validateCustomerInformationExists(id);
        // 删除
        customerInformationMapper.deleteById(id);
    }

    private void validateCustomerInformationExists(Long id) {
        if (customerInformationMapper.selectById(id) == null) {
            throw exception(CUSTOMER_INFORMATION_NOT_EXISTS);
        }
    }

    @Override
    public CustomerInformationDO getCustomerInformation(Long id) {
        return customerInformationMapper.selectById(id);
    }

    @Override
    public PageResult<CustomerInformationDO> getCustomerInformationPage(CustomerInformationPageReqVO pageReqVO) {
        return customerInformationMapper.selectPage(pageReqVO);
    }

    @Override
    public CustomerInformationDO byCustomerIdGetInformation(Long customerId) {
        return customerInformationMapper.selectOne(CustomerInformationDO::getCustomerId,customerId);
    }

    @Override
    public void byCustomerIdDeleteInformation(Long customerId) {
         customerInformationMapper.delete(CustomerInformationDO::getCustomerId,customerId);
    }

}